package com.beiding.jsonanalyzer.querylink;

import com.beiding.jsonanalyzer.analyze.func.FuncBase;
import com.beiding.jsonanalyzer.analyze.func.FuncFactory;
import com.beiding.jsonanalyzer.analyze.query.ComparatorResult;
import com.beiding.jsonanalyzer.flow.BranchNode;
import com.beiding.jsonanalyzer.flow.ValueNode;
import com.beiding.jsonanalyzer.flow.flowevents.DataNodeEvent;

public class FindQueryLinkNode<W extends ValueNode> extends ComputeNode<W> {


    private FuncFactory<FuncBase<ComparatorResult>> query;


    public FindQueryLinkNode(QueryLinkNode<W> preNode, FuncFactory<FuncBase<ComparatorResult>> query) {
        super(preNode);
        this.query=query;
    }

    @Override
    protected void bindByComputeNode(BranchNode<W> preResult, BranchNode<W> newBranchNode) {
        FuncBase<ComparatorResult> queryInstance = query.createInstance();

        //不支持聚合式

        ComputeNodeUtils.bindControlEvent(preResult, newBranchNode);
        preResult.onDataEvent(DataNodeEvent.eventType(), event -> {
            W dataNode = event.getData();
            if (queryInstance.executeOne(dataNode) == ComparatorResult.TRUE) {
                newBranchNode.signDataEvent(event);
            }
        });

    }


}
